package com.example.demo.story.impl;

import com.example.demo.story.NetStoryServiceAbstract;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Objects;

/**
 * @Author TCJ
 * @create 2023/10/30
 */
public class DingDianStoryServiceImpl extends NetStoryServiceAbstract {
    @Override
    public String getBaseUrl() {
        return "https://www.xxbooktxt.com";
    }

    @Override
    public String getReadUrl() {
        return getBaseUrl() + "/85059_85059832/";
    }

    @Override
    public void analysisChapter() {
        Document html = super.getHtml(getReadUrl());
        Element list = html.body().getElementById("list");
        if (list == null){
            return;
        }
        Elements dt = list.getElementsByTag("dt");
        super.dto.setTitle(dt.text());
        Elements dd = list.getElementsByTag("dd");
        dd.forEach(item ->{
            Elements a = item.getElementsByTag("a");
            super.dto.getChapter().put(a.text(),getBaseUrl() + a.attr("href"));
        });
    }

    @Override
    public String getContent(String url) {
        Document html = super.getHtml(url);
        System.out.println(html.body().getElementsByClass("bookname").text());
        return Objects.requireNonNull(html.body().getElementById("content")).text();
    }
}
